package com.briup.day20;

public class Test_Join {
    public static void main(String[] args) {
        Thread th1 = new Thread("t1线程") {
            public void run() {
                for (int i = 0; i <= 40; i++) {
                    String name = Thread.currentThread().getName();
                    System.out.println(name + "i:" + i);
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        };
        //创建th2线程
        Thread th2 = new Thread("t2线程") {
            public void run() {
                for (int i = 100; i >= 70; i--) {
                    String name = Thread.currentThread().getName();
                    System.out.println(name + "i:" + i);
                    try {
                        Thread.sleep(10);
                        if (i == 80) {
                            th1.join();
//                            th1.join(100);
                        }
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        };
        th1.start();
        th2.start();
        //主线程：每隔7ms输出子线程2状态
        for (int i = 0; i < 50; i++) {
            System.out.println("主线程, i: " + i + " th2状态: " + th2.getState());
            try {
                Thread.sleep(7);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
